package com.micro.modual.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micro.common.MicroResponseCode;
import com.micro.common.util.JacksonUtil;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.page.PageQuery;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonData;
import com.micro.core.resp.RespJsonPageData;
import com.micro.modual.entity.GoodsBrand;
import com.micro.modual.entity.GoodsBrandCategory;
import com.micro.modual.entity.GoodsCategory;
import com.micro.modual.extension.GoodsBrandCategoryExtension;
import com.micro.modual.mapper.GoodsBrandCategoryMapper;
import com.micro.modual.mapper.GoodsBrandMapper;
import com.micro.modual.mapper.GoodsCategoryMapper;
import com.micro.modual.model.bo.goodsbrandcategory.GoodsBrandCategoryBO;
import com.micro.modual.model.bo.relation.RelationBrandCategoryBO;
import com.micro.modual.model.dto.relation.RelationBrandCategoryListDTO;
import com.micro.modual.service.GoodsBrandCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
*  <h1>商品品牌分类关联表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("goodsBrandCategoryService")
public class GoodsBrandCategoryServiceImpl extends AbstractBasicsServiceImpl<GoodsBrandCategory,Long> implements GoodsBrandCategoryService {

    @Autowired
    private GoodsBrandMapper goodsBrandMapper;

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Autowired
    private GoodsBrandCategoryMapper goodsBrandCategoryMapper;

    /**
     * <h2>根据品牌编号查询分类关联信息</h2>
     * @param map  请求对象
     * @return
     */
    @Override
    public RespJsonPageData<RelationBrandCategoryListDTO> findRelationBrandCategoryPageList(Map<String,Object> map) {
        PageQuery pageQuery= new PageQuery(map);
        Page<Object> result = PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        List<GoodsBrandCategory> goodsBrandCategoryList = goodsBrandCategoryMapper.findRelationBrandCategoryList(map);
        List<RelationBrandCategoryListDTO> relationBrandCategoryListDTOList = JacksonUtil.getList(JacksonUtil.toJson(goodsBrandCategoryList),RelationBrandCategoryListDTO.class);
        return new RespJsonPageData<RelationBrandCategoryListDTO>(relationBrandCategoryListDTOList,result.getTotal(), MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * 新增品牌分类关联
     * @param relationBrandCategoryBO
     * @return
     */
    @Override
    public RespJson addRelationBrandCategory(RelationBrandCategoryBO relationBrandCategoryBO) {
        if(goodsBrandCategoryMapper.isExist(relationBrandCategoryBO.getCategoryCode(),relationBrandCategoryBO.getBrandCode() ) > 0){
            throw new BizException(MicroResponseCode.GOODS_BRAND_CATEGORY_HAS_EXIST.getResponseCode(),MicroResponseCode.GOODS_BRAND_CATEGORY_HAS_EXIST.getResponseMessage());
        }
        GoodsBrand goodsBrand = goodsBrandMapper.findBrandByCode(relationBrandCategoryBO.getBrandCode());
        GoodsCategory goodsCategory = goodsCategoryMapper.findGoodsCateGoryByCode(relationBrandCategoryBO.getCategoryCode());
        GoodsBrandCategory goodsBrandCategory = GoodsBrandCategoryExtension.converAddGoodsBrandCategory(goodsBrand,goodsCategory);
        goodsBrandCategoryMapper.insertSelective(goodsBrandCategory);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>查询品牌分类关联信息-无分页</h2>
     * @param map 请求对象
     * @return
     */
    @Override
    public RespJsonData<List<RelationBrandCategoryListDTO>> findRelationBrandCategoryList(Map<String,Object> map) {
        List<GoodsBrandCategory> goodsBrandCategoryList = goodsBrandCategoryMapper.findRelationBrandCategoryList(map);
        List<RelationBrandCategoryListDTO> relationBrandCategoryListDTOList = JacksonUtil.getList(JacksonUtil.toJson(goodsBrandCategoryList),RelationBrandCategoryListDTO.class);
        return new RespJsonData<List<RelationBrandCategoryListDTO>>(relationBrandCategoryListDTOList, MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

}
